//发送 ajax 请求
function myAjax(apiName,data,callback){
    url = '';
    json = {
        Head:{
            ApiName:apiName
        },
        Body:data
    };
    if(url.length == 0){
        callback(postData[apiName])
    }else{
        $.post(url,json,function(data){
            console.log(data);
            callback(data);
        })
    }
}
//获取一组 checkbox 已选值
function getCheckBoxValue(sel){
    els = $(sel);
    if(els.length>0){
        vs = [];
        els.each(function(){
            if(this.checked == true){
                vs.push(this.value);
            }
        })
        if(vs.length > 0){
            return vs;
        }
    }
    return false;
}

//设置一组 checkbox 的值
function setCheckBoxValue(sel,bool){
    els = $(sel);
    console.log(els)
    if(els.length>0){
        els.each(function(){
            this.checked = bool;
        })
    }
    if(bool){
        return getCheckBoxValue(els)
    }else{
        return false;
    }
}

//通过数据编号取特定数据集合
function getDataByid(data,ids,key){
    res = [];
    for(i = 0;i<ids.length;i++){
        id = ids[i]
        if(data.length > id){
            json = data[id];
            res.push(json[key])
        }
    }
    if(res.length>0){
        return res;
    }
    return false;
}

//模态弹框 返回弹出的 Jquery 对象
function createModeDialog(id,cls,title){
    if(title == null){title = '模态弹框'}
    temp = `
    <div id='` + id + `' class='mode_dialog'>
        <div class='dialog'>
            <div class="dialog_head">
                <b class="dialog_title">` + title + `</b>
                <b class="dialog_close">关闭</b>
            </div>
            <div class="dialog_body ` + cls + `"></div>
        </div>
    </div>
    `;
    $("body").append(temp);
    $("#"+id).width($(window).width()).height($(window).height())
    dialog = $("#" + id + ">.dialog");
    dialog.css({
        left:($(window).width() - dialog.width())/2 + "px",
        top:($(window).height() - dialog.height())/2 + "px"
    });
    $("#" + id + " .dialog_body").css({
        height:(dialog.height() - $("#" + id + " .dialog_head").height()) + "px"
    })
    $("#" + id + " .dialog_close").click(function(){
        $("#"+id).remove()
    })
    return $("#" + id + " .dialog_body");
}

//非模态弹框
function createDialog(parent,cls,id){
    temp = `
    <div id='` + id + `' class='` + cls + `' ></div>
    `;
    $(parent).append(temp);
    return $("#" + id);
}
//模板变量替换 data 必须是 array 型
function replaceTemp(data,temp){
    if(!$.isArray(data) && $.isPlainObject(data)){
        data = [data];
    }
    if(data.length>0){
        reg = new RegExp("\\$[\\w\\+\\-]+\\$",'g');
        ms = temp.match(reg)
        console.log(ms)
        res = "";
        if(ms != null && ms.length > 0){
            for(i = 0;i<data.length;i++){
                str = temp;
                json = data[i];
                json["i"] = i;
                json["i+1"] = i+1;
                for(j = 0;j<ms.length;j++){
                    key = ms[j];
                    k = key.substring(1,key.length - 1);
                    val = "";
                    if(json[k] != null){
                        val = json[k];
                    }
                    str = str.replace(key,val);
                }
                res += str;
            }
            
        }else{
            res = temp;
        }
        return res;
    }
    return false;
}

//添加开关事件
function addSwitchEvent(id,callback,def){
    if(def == undefined){def = 0}else{def = 1;}
    temp = `<input type='hidden' name='switch_value' value='`+ def + `'>`
    swt = $("#" + id);
    if(swt.length > 0){
        swt.append(temp);
        if(def == 1){
            swt.addClass("switch_on")
        }
        swt.click(function(){
            input = swt.children("input[name=switch_value]");
            val = input.val();
            if(val == 0){
                swt.addClass("switch_on");
                input.val("1");
            }
            if(val == 1){
                swt.removeClass("switch_on");
                input.val("0");
            }
        })
    }
   
}

//添加切换按纽事件
function addTriggerEvent(name,callback){

    // console.log( $(".trigger[name=" + name + "]"))
    $(".trigger[name=" + name + "]").each(function(){
        val = $(this).attr("val");
        def = $(this).attr("def");
        if(val == def){
			console.log(val,def)
            $(this).css({})
        }else{
            $(this).css({display:"none"})
        }

    });
    $(".trigger[name=" + name + "]").click(callback);
}

//生成 随机 key  和 screct
function createKeyOrScrect(len){
    res = '';
    ch = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    chs = ch.split('')
    for(i = 0;i<len;i++){
        id = Math.ceil(Math.random() * (chs.length - 1))
        res += chs[id];
    }
    return res;
}
//追加  menu list 
function addMenuList(){
    $(".menu").append(templateMenuList);
}

//数组删除指定索引,返回新数组
function arrayDelete(arr,ins){
    res = [];
    for(i=0;i<arr.length;i++){
        if(!ins.includes(i+"")){
            res.push(arr[i])
        }
    }
    return res;
}

